package domain;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.Writable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Tweet implements Writable{
	
	private User user;
	private String text;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	@Override
	public void readFields(DataInput arg0) throws IOException {
		parseTweetFromString(arg0.toString());
	}

	@Override
	public void write(DataOutput arg0) throws IOException {
		arg0.writeUTF(this.toString());
	}

	
	@Override
	public String toString(){
		return this.user.toString() + " - " + this.text;
	}
	
	public static Tweet parseTweetFromString(String data){
		Tweet staticTweet = new Tweet();
		staticTweet.user = new User();
		try {
			JSONObject myjSON = new JSONObject(data);
			
			staticTweet.setUser(User.parseUserFromString(data));
			
			if(myjSON.has("text")){
				staticTweet.setText(myjSON.getString("text"));		
			}else{
				staticTweet.setText("unknown");		
			}	
				
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return staticTweet;
	}

}

